$def with (course,data,errors,webdav_host)

$#
$# This file is part of INGInious. See the LICENSE and the COPYRIGHTS files for
$# more information about the licensing of this file.
$#

$var title: $:course.get_name(user_manager.session_language())

$var Column: $:template_helper.call('course_admin_menu',course=course,current='tasks')
$ is_admin = user_manager.has_admin_rights_on_course(course)

$def NavbarF():
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="$get_homepath()/course/$course.get_id()">$course.get_name(user_manager.session_language())</a></li>
            <li class="breadcrumb-item"><a href="$get_homepath()/admin/$course.get_id()" title=$:_('"Administration"') data-toggle="tooltip" data-placement="bottom">
                <i class="fa fa-user-secret"></i></a></li>
            <li class="breadcrumb-item active"><a href="#"><i class="fa fa-tasks"></i> $:_("Tasks") <span class="sr-only">$:_("(current)")</span></a></li>
        </ol>
    </nav>
$var Navbar: $:NavbarF()

<form method="post">
<h2>$:_("Tasks")
    <div class="pull-right">
        <div class="btn-group btn-group-sm">
            <button type="button" id="switch" class="btn btn-default"><i class="fa fa-list-ol"></i> $:_("Change task order")</button>
            <button type="submit" id="switch" class="btn btn-success"><i class="fa fa-check"></i> $:_("Update task order")</button>
        </div>
    </div>
</h2>

$if webdav_host:
    <div class="mb-3 mt-3" id="webdav_accord" role="tablist" aria-multiselectable="true">
        <div class="card">
            <div class="card-header" id="webdav_head">
                <div style="cursor: pointer" data-toggle="collapse" data-parent="#webdav_accord" href="#webdav" aria-expanded="false" aria-controls="webdav">
                    <i class="fa fa-fw fa-chevron-right"></i>$:_("WebDAV access")
                </div>
            </div>
            <div id="webdav" class="collapse" role="tabpanel" aria-labelledby="webdav_head">
                <div class="card-body">
                    $:_("Use this URL to access your course folder using WebDav:")<br/>
                <br/>
                <table class="table">
                    <tr><td>$:_("URL")</td><td><code>$(webdav_host + "/" + course.get_id())</code></td></tr>
                    <tr><td>$:_("Username")</td><td><code>$user_manager.session_username()</code></td></tr>
                    <tr><td>$:_("Password")</td><td><code>$user_manager.session_api_key()</code></td></tr>
                </table>
                </div>
            </div>
        </div>
    </div>

<table class="table sorted_table">
    <thead class="table-borderless">
        <tr>
            <th>$:_("task name")</th>
            <th>$:_("# student viewed")</th>
            <th>$:_("# student attempted")</th>
            <th>$:_("# student succeeded")</th>
            <th>$:_("# attempts")</th>
            <th>
                <div class="btn-group btn-group-sm" role="group">
                    <a href="$get_homepath()/admin/${course.get_id()}/download?format=taskid%2Fusername" class="btn btn-danger"
                       title=$:_('"Download all submissions"') data-toggle="tooltip" data-placement="bottom"><i class="fa fa-download"></i></a>
                    <a href="$get_homepath()/admin/${course.get_id()}/tasks?csv" class="btn btn-info"
                       title=$:_('"Download CSV"') data-toggle="tooltip" data-placement="bottom"><i class="fa fa-table"></i></a>
                </div>
            </th>
        </tr>
    </thead>
    <tbody>
    $for taskid in data:
    	<tr>
    		<td>
    			$data[taskid]["name"]
                <input type="hidden" name="task" value="$taskid">
    		</td>
    		<td>$data[taskid]["viewed"]</td>
    		<td>$data[taskid]["attempted"]</td>
            <td>$data[taskid]["succeeded"]</td>
            <td>$data[taskid]["attempts"]</td>
    		<td style="min-width:120px">
                <div class="btn-group btn-group-sm" role="group">
                    $if is_admin:
                        <a href="$get_homepath()/admin/${course.get_id()}/edit/task/$taskid" class="btn btn-primary" title=$:_('"Edit task"') data-toggle="tooltip"
                           data-placement="bottom"><i class="fa fa-edit"></i></a>
                    <a href="$get_homepath()/admin/${course.get_id()}/submissions$data[taskid]['url']" class="btn btn-secondary" title=$:_('"View results"') data-toggle="tooltip"
                       data-placement="bottom"><i class="fa fa-users"></i></a>
                    <a href="$get_homepath()/admin/${course.get_id()}/download$data[taskid]['url']" class="btn btn-secondary" title=$:_('"Download submissions"')
                       data-toggle="tooltip" data-placement="bottom"><i class="fa fa-download"></i></a>
                    $if is_admin:
                        <a href="$get_homepath()/admin/${course.get_id()}/replay$data[taskid]['url']" class="btn btn-secondary" title=$:_('"Replay submissions"')
                           data-toggle="tooltip" data-placement="bottom"><i class="fa fa-refresh"></i></a>
                </div>
    		</td>
    	</tr>
    </tbody>
</table>
</form>


<div class="form-group row">
        <div class="col-sm-9"><input type="text" class="form-control" id="new_task_id" placeholder=$:_('"New task id"')/></div>
        <div class="col-sm-3"><a href="javascript:studio_create_new_task();" class="btn btn-info btn-large btn-block">$:_("Create new task")</a></div>
</div>
$if len(errors) != 0:
    <h3>$:_("Errors while loading tasks")</h3>
    <table class="table">
        <thead class="table-borderless">
            <th>$:_("task id")</th>
            <th>$:_("error")</th>
        </thead>
        $for task in errors:
            <tr>
                <td>$task['taskid']</td>
                <td><pre>$task['error']</pre></td>
            </tr>
    </table>

<script type="text/javascript">
$$('.sorted_table').sortable({
    containerSelector: 'table',
    itemPath: '> tbody',
    itemSelector: 'tr',
    placeholder: '<tr class="placeholder"/>'
});

$$('.sorted_table').sortable("disable");

$$("#switch").click(function  (e) {
    if($$(this).hasClass("active")) {
        $$(this).removeClass("active");
        $$(".sorted_table tr").removeAttr("style");
    } else {
        $$(this).addClass("active");
        $$(".sorted_table tr").attr("style", "cursor: pointer;");
    }
    $$('.sorted_table').sortable($$(this).hasClass("active") ? "enable" : "disable");
});
</script>